package chen.web.widget.vote;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import chen.web.widget.Widget;

public class Vote extends Widget{
	
	/**
	 * 选项数量上限
	 */
	public static final int MaxOptionalQuantity = 50;
	
	private String title;
	private boolean publicVoters = false;
	private Date dateline;
	private Date deadline;
	private int optionalQuantity = 1;
	private int flag;
	private List<VoteOption> options = new ArrayList<VoteOption>();
	private String[] optionsContent = null;
	
	public Vote(int id) {
		super(id);
		// TODO Auto-generated constructor stub
	}
	
	public Vote(){
		super(0);
	}
	
	public int getId() {
		return id;
	}
	void setId(int id) {
		this.id = id;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	}
	public boolean isPublicVoters() {
		return publicVoters;
	}
	public void setPublicVoters(boolean publicVoters) {
		this.publicVoters = publicVoters;
	}
	public int getOptionalQuantity() {
		return optionalQuantity;
	}
	public void setOptionalQuantity(int optionalQuantity) {
		if(optionalQuantity < 1){
			throw new IllegalArgumentException();
		}
		this.optionalQuantity = optionalQuantity;
	}
	int getFlag() {
		return flag;
	}
	void setFlag(int flag) {
		this.flag = flag;
	}
	public List<VoteOption> getOptions() {
		return options;
	}
	public void setDateline(Date dateline) {
		this.dateline = dateline;
	}
	public Date getDateline() {
		return dateline;
	}
	public void setDeadline(Date deadline) {
		this.deadline = deadline;
	}
	public Date getDeadline() {
		return deadline;
	}
	void addOption(VoteOption option){
		options.add(option);
	}
	
	/**
	 * 设置投票选项
	 * @param contents
	 */
	public void setOptionsContent(String... contents){
		if(contents.length > MaxOptionalQuantity){
			throw new IllegalArgumentException();
		}
		optionsContent = contents;
	}
	
	String[] getOptionsContent(){
		return optionsContent;
	}
	
	/**
	 * 是否已经有人投票了
	 * @return
	 */
	public boolean hasVoted(){
		for(VoteOption option : options){
			if(option.getCount() != 0){
				return true;
			}
		}
		return false;
	}
	
	public int getVoteAmount(){
		int amount = 0;
		for(VoteOption option : options){
			amount = amount + option.getCount();
		}
		return amount;
	}
}
